{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "ba5d361c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0577fe5d",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "317f3247",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = load_iris().data\n",
    "Y = load_iris().target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "07f171c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[5.1, 3.5, 1.4, 0.2],\n",
       "        [4.9, 3. , 1.4, 0.2],\n",
       "        [4.7, 3.2, 1.3, 0.2],\n",
       "        [4.6, 3.1, 1.5, 0.2],\n",
       "        [5. , 3.6, 1.4, 0.2],\n",
       "        [5.4, 3.9, 1.7, 0.4],\n",
       "        [4.6, 3.4, 1.4, 0.3],\n",
       "        [5. , 3.4, 1.5, 0.2],\n",
       "        [4.4, 2.9, 1.4, 0.2],\n",
       "        [4.9, 3.1, 1.5, 0.1],\n",
       "        [5.4, 3.7, 1.5, 0.2],\n",
       "        [4.8, 3.4, 1.6, 0.2],\n",
       "        [4.8, 3. , 1.4, 0.1],\n",
       "        [4.3, 3. , 1.1, 0.1],\n",
       "        [5.8, 4. , 1.2, 0.2],\n",
       "        [5.7, 4.4, 1.5, 0.4],\n",
       "        [5.4, 3.9, 1.3, 0.4],\n",
       "        [5.1, 3.5, 1.4, 0.3],\n",
       "        [5.7, 3.8, 1.7, 0.3],\n",
       "        [5.1, 3.8, 1.5, 0.3],\n",
       "        [5.4, 3.4, 1.7, 0.2],\n",
       "        [5.1, 3.7, 1.5, 0.4],\n",
       "        [4.6, 3.6, 1. , 0.2],\n",
       "        [5.1, 3.3, 1.7, 0.5],\n",
       "        [4.8, 3.4, 1.9, 0.2],\n",
       "        [5. , 3. , 1.6, 0.2],\n",
       "        [5. , 3.4, 1.6, 0.4],\n",
       "        [5.2, 3.5, 1.5, 0.2],\n",
       "        [5.2, 3.4, 1.4, 0.2],\n",
       "        [4.7, 3.2, 1.6, 0.2],\n",
       "        [4.8, 3.1, 1.6, 0.2],\n",
       "        [5.4, 3.4, 1.5, 0.4],\n",
       "        [5.2, 4.1, 1.5, 0.1],\n",
       "        [5.5, 4.2, 1.4, 0.2],\n",
       "        [4.9, 3.1, 1.5, 0.2],\n",
       "        [5. , 3.2, 1.2, 0.2],\n",
       "        [5.5, 3.5, 1.3, 0.2],\n",
       "        [4.9, 3.6, 1.4, 0.1],\n",
       "        [4.4, 3. , 1.3, 0.2],\n",
       "        [5.1, 3.4, 1.5, 0.2],\n",
       "        [5. , 3.5, 1.3, 0.3],\n",
       "        [4.5, 2.3, 1.3, 0.3],\n",
       "        [4.4, 3.2, 1.3, 0.2],\n",
       "        [5. , 3.5, 1.6, 0.6],\n",
       "        [5.1, 3.8, 1.9, 0.4],\n",
       "        [4.8, 3. , 1.4, 0.3],\n",
       "        [5.1, 3.8, 1.6, 0.2],\n",
       "        [4.6, 3.2, 1.4, 0.2],\n",
       "        [5.3, 3.7, 1.5, 0.2],\n",
       "        [5. , 3.3, 1.4, 0.2],\n",
       "        [7. , 3.2, 4.7, 1.4],\n",
       "        [6.4, 3.2, 4.5, 1.5],\n",
       "        [6.9, 3.1, 4.9, 1.5],\n",
       "        [5.5, 2.3, 4. , 1.3],\n",
       "        [6.5, 2.8, 4.6, 1.5],\n",
       "        [5.7, 2.8, 4.5, 1.3],\n",
       "        [6.3, 3.3, 4.7, 1.6],\n",
       "        [4.9, 2.4, 3.3, 1. ],\n",
       "        [6.6, 2.9, 4.6, 1.3],\n",
       "        [5.2, 2.7, 3.9, 1.4],\n",
       "        [5. , 2. , 3.5, 1. ],\n",
       "        [5.9, 3. , 4.2, 1.5],\n",
       "        [6. , 2.2, 4. , 1. ],\n",
       "        [6.1, 2.9, 4.7, 1.4],\n",
       "        [5.6, 2.9, 3.6, 1.3],\n",
       "        [6.7, 3.1, 4.4, 1.4],\n",
       "        [5.6, 3. , 4.5, 1.5],\n",
       "        [5.8, 2.7, 4.1, 1. ],\n",
       "        [6.2, 2.2, 4.5, 1.5],\n",
       "        [5.6, 2.5, 3.9, 1.1],\n",
       "        [5.9, 3.2, 4.8, 1.8],\n",
       "        [6.1, 2.8, 4. , 1.3],\n",
       "        [6.3, 2.5, 4.9, 1.5],\n",
       "        [6.1, 2.8, 4.7, 1.2],\n",
       "        [6.4, 2.9, 4.3, 1.3],\n",
       "        [6.6, 3. , 4.4, 1.4],\n",
       "        [6.8, 2.8, 4.8, 1.4],\n",
       "        [6.7, 3. , 5. , 1.7],\n",
       "        [6. , 2.9, 4.5, 1.5],\n",
       "        [5.7, 2.6, 3.5, 1. ],\n",
       "        [5.5, 2.4, 3.8, 1.1],\n",
       "        [5.5, 2.4, 3.7, 1. ],\n",
       "        [5.8, 2.7, 3.9, 1.2],\n",
       "        [6. , 2.7, 5.1, 1.6],\n",
       "        [5.4, 3. , 4.5, 1.5],\n",
       "        [6. , 3.4, 4.5, 1.6],\n",
       "        [6.7, 3.1, 4.7, 1.5],\n",
       "        [6.3, 2.3, 4.4, 1.3],\n",
       "        [5.6, 3. , 4.1, 1.3],\n",
       "        [5.5, 2.5, 4. , 1.3],\n",
       "        [5.5, 2.6, 4.4, 1.2],\n",
       "        [6.1, 3. , 4.6, 1.4],\n",
       "        [5.8, 2.6, 4. , 1.2],\n",
       "        [5. , 2.3, 3.3, 1. ],\n",
       "        [5.6, 2.7, 4.2, 1.3],\n",
       "        [5.7, 3. , 4.2, 1.2],\n",
       "        [5.7, 2.9, 4.2, 1.3],\n",
       "        [6.2, 2.9, 4.3, 1.3],\n",
       "        [5.1, 2.5, 3. , 1.1],\n",
       "        [5.7, 2.8, 4.1, 1.3],\n",
       "        [6.3, 3.3, 6. , 2.5],\n",
       "        [5.8, 2.7, 5.1, 1.9],\n",
       "        [7.1, 3. , 5.9, 2.1],\n",
       "        [6.3, 2.9, 5.6, 1.8],\n",
       "        [6.5, 3. , 5.8, 2.2],\n",
       "        [7.6, 3. , 6.6, 2.1],\n",
       "        [4.9, 2.5, 4.5, 1.7],\n",
       "        [7.3, 2.9, 6.3, 1.8],\n",
       "        [6.7, 2.5, 5.8, 1.8],\n",
       "        [7.2, 3.6, 6.1, 2.5],\n",
       "        [6.5, 3.2, 5.1, 2. ],\n",
       "        [6.4, 2.7, 5.3, 1.9],\n",
       "        [6.8, 3. , 5.5, 2.1],\n",
       "        [5.7, 2.5, 5. , 2. ],\n",
       "        [5.8, 2.8, 5.1, 2.4],\n",
       "        [6.4, 3.2, 5.3, 2.3],\n",
       "        [6.5, 3. , 5.5, 1.8],\n",
       "        [7.7, 3.8, 6.7, 2.2],\n",
       "        [7.7, 2.6, 6.9, 2.3],\n",
       "        [6. , 2.2, 5. , 1.5],\n",
       "        [6.9, 3.2, 5.7, 2.3],\n",
       "        [5.6, 2.8, 4.9, 2. ],\n",
       "        [7.7, 2.8, 6.7, 2. ],\n",
       "        [6.3, 2.7, 4.9, 1.8],\n",
       "        [6.7, 3.3, 5.7, 2.1],\n",
       "        [7.2, 3.2, 6. , 1.8],\n",
       "        [6.2, 2.8, 4.8, 1.8],\n",
       "        [6.1, 3. , 4.9, 1.8],\n",
       "        [6.4, 2.8, 5.6, 2.1],\n",
       "        [7.2, 3. , 5.8, 1.6],\n",
       "        [7.4, 2.8, 6.1, 1.9],\n",
       "        [7.9, 3.8, 6.4, 2. ],\n",
       "        [6.4, 2.8, 5.6, 2.2],\n",
       "        [6.3, 2.8, 5.1, 1.5],\n",
       "        [6.1, 2.6, 5.6, 1.4],\n",
       "        [7.7, 3. , 6.1, 2.3],\n",
       "        [6.3, 3.4, 5.6, 2.4],\n",
       "        [6.4, 3.1, 5.5, 1.8],\n",
       "        [6. , 3. , 4.8, 1.8],\n",
       "        [6.9, 3.1, 5.4, 2.1],\n",
       "        [6.7, 3.1, 5.6, 2.4],\n",
       "        [6.9, 3.1, 5.1, 2.3],\n",
       "        [5.8, 2.7, 5.1, 1.9],\n",
       "        [6.8, 3.2, 5.9, 2.3],\n",
       "        [6.7, 3.3, 5.7, 2.5],\n",
       "        [6.7, 3. , 5.2, 2.3],\n",
       "        [6.3, 2.5, 5. , 1.9],\n",
       "        [6.5, 3. , 5.2, 2. ],\n",
       "        [6.2, 3.4, 5.4, 2.3],\n",
       "        [5.9, 3. , 5.1, 1.8]]),\n",
       " array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]))"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X,Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "d677912f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((150, 4), (150,))"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape,Y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "aff18872",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = pd.DataFrame(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "d87b361a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       0    1    2    3\n",
       "0    5.1  3.5  1.4  0.2\n",
       "1    4.9  3.0  1.4  0.2\n",
       "2    4.7  3.2  1.3  0.2\n",
       "3    4.6  3.1  1.5  0.2\n",
       "4    5.0  3.6  1.4  0.2\n",
       "..   ...  ...  ...  ...\n",
       "145  6.7  3.0  5.2  2.3\n",
       "146  6.3  2.5  5.0  1.9\n",
       "147  6.5  3.0  5.2  2.0\n",
       "148  6.2  3.4  5.4  2.3\n",
       "149  5.9  3.0  5.1  1.8\n",
       "\n",
       "[150 rows x 4 columns]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "160b6fae",
   "metadata": {},
   "source": [
    "# 切分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "14b28c82",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xtrain,Xtest,Ytrain,Ytest =  train_test_split(X,Y,test_size=0.3,random_state=420)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "84b723f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(       0    1    2    3\n",
       " 110  6.5  3.2  5.1  2.0\n",
       " 87   6.3  2.3  4.4  1.3\n",
       " 34   4.9  3.1  1.5  0.2\n",
       " 125  7.2  3.2  6.0  1.8\n",
       " 1    4.9  3.0  1.4  0.2\n",
       " ..   ...  ...  ...  ...\n",
       " 115  6.4  3.2  5.3  2.3\n",
       " 31   5.4  3.4  1.5  0.4\n",
       " 63   6.1  2.9  4.7  1.4\n",
       " 134  6.1  2.6  5.6  1.4\n",
       " 72   6.3  2.5  4.9  1.5\n",
       " \n",
       " [105 rows x 4 columns],\n",
       "        0    1    2    3\n",
       " 66   5.6  3.0  4.5  1.5\n",
       " 104  6.5  3.0  5.8  2.2\n",
       " 105  7.6  3.0  6.6  2.1\n",
       " 2    4.7  3.2  1.3  0.2\n",
       " 7    5.0  3.4  1.5  0.2\n",
       " 67   5.8  2.7  4.1  1.0\n",
       " 27   5.2  3.5  1.5  0.2\n",
       " 90   5.5  2.6  4.4  1.2\n",
       " 138  6.0  3.0  4.8  1.8\n",
       " 91   6.1  3.0  4.6  1.4\n",
       " 139  6.9  3.1  5.4  2.1\n",
       " 84   5.4  3.0  4.5  1.5\n",
       " 22   4.6  3.6  1.0  0.2\n",
       " 25   5.0  3.0  1.6  0.2\n",
       " 112  6.8  3.0  5.5  2.1\n",
       " 82   5.8  2.7  3.9  1.2\n",
       " 42   4.4  3.2  1.3  0.2\n",
       " 6    4.6  3.4  1.4  0.3\n",
       " 11   4.8  3.4  1.6  0.2\n",
       " 46   5.1  3.8  1.6  0.2\n",
       " 20   5.4  3.4  1.7  0.2\n",
       " 145  6.7  3.0  5.2  2.3\n",
       " 89   5.5  2.5  4.0  1.3\n",
       " 12   4.8  3.0  1.4  0.1\n",
       " 54   6.5  2.8  4.6  1.5\n",
       " 56   6.3  3.3  4.7  1.6\n",
       " 39   5.1  3.4  1.5  0.2\n",
       " 33   5.5  4.2  1.4  0.2\n",
       " 81   5.5  2.4  3.7  1.0\n",
       " 58   6.6  2.9  4.6  1.3\n",
       " 93   5.0  2.3  3.3  1.0\n",
       " 123  6.3  2.7  4.9  1.8\n",
       " 124  6.7  3.3  5.7  2.1\n",
       " 49   5.0  3.3  1.4  0.2\n",
       " 17   5.1  3.5  1.4  0.3\n",
       " 51   6.4  3.2  4.5  1.5\n",
       " 136  6.3  3.4  5.6  2.4\n",
       " 64   5.6  2.9  3.6  1.3\n",
       " 70   5.9  3.2  4.8  1.8\n",
       " 55   5.7  2.8  4.5  1.3\n",
       " 80   5.5  2.4  3.8  1.1\n",
       " 97   6.2  2.9  4.3  1.3\n",
       " 53   5.5  2.3  4.0  1.3\n",
       " 83   6.0  2.7  5.1  1.6\n",
       " 116  6.5  3.0  5.5  1.8)"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xtrain,Xtest,"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34dc74e7",
   "metadata": {},
   "source": [
    "# 数据集标准化，训练集学习，测试集得分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "80c145a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.98926548e-01,  3.11391996e-01,  7.07721645e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 4.65951032e-01, -1.67688798e+00,  3.19241130e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.16487758e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 1.51434085e+00,  3.11391996e-01,  1.20719659e+00,\n",
       "         7.36367223e-01],\n",
       "       [-1.16487758e+00, -1.30447998e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01,  7.53231990e-01,  8.74213294e-01,\n",
       "         1.38501285e+00],\n",
       "       [-4.65951032e-01,  9.74151987e-01, -1.40117258e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-9.31902064e-01,  1.63691198e+00, -1.29017814e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.16487758e-01, -5.72287992e-01,  7.07721645e-01,\n",
       "         1.51474197e+00],\n",
       "       [ 2.32975516e-01, -1.30447998e-01,  5.96727212e-01,\n",
       "         7.36367223e-01],\n",
       "       [-8.15414306e-01,  2.29967197e+00, -1.29017814e+00,\n",
       "        -1.46902790e+00],\n",
       "       [ 2.32975516e+00,  1.63691198e+00,  1.42918546e+00,\n",
       "         9.95825472e-01],\n",
       "       [ 1.28136534e+00,  3.11391996e-01,  4.85732779e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 1.04838982e+00,  3.11391996e-01,  1.15169938e+00,\n",
       "         1.38501285e+00],\n",
       "       [-3.49463274e-01, -1.23504798e+00,  4.17550485e-02,\n",
       "        -1.71736651e-01],\n",
       "       [ 2.32975516e-01, -5.72287992e-01,  9.72522648e-02,\n",
       "         8.77215987e-02],\n",
       "       [ 9.31902064e-01,  5.32311993e-01,  1.04070494e+00,\n",
       "         1.64447110e+00],\n",
       "       [-1.28136534e+00, -1.30447998e-01, -1.34567536e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.28136534e+00,  7.53231990e-01, -1.06818928e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-2.32975516e-01, -1.23504798e+00,  6.52224428e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 2.09677965e+00, -5.72287992e-01,  1.59567711e+00,\n",
       "         9.95825472e-01],\n",
       "       [ 1.16487758e+00,  9.04719988e-02,  7.07721645e-01,\n",
       "         1.38501285e+00],\n",
       "       [ 1.39785310e+00, -1.30447998e-01,  1.15169938e+00,\n",
       "         1.12555460e+00],\n",
       "       [-9.31902064e-01,  9.74151987e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.86380413e+00, -1.30447998e-01, -1.51216701e+00,\n",
       "        -1.46902790e+00],\n",
       "       [-2.32975516e-01,  1.63691198e+00, -1.17918371e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-9.31902064e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 1.16487758e-01, -1.89780797e+00,  6.52224428e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.04838982e+00,  1.19507198e+00, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.74731637e+00, -1.30447998e-01, -1.40117258e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 2.09677965e+00, -1.30447998e-01,  1.26269381e+00,\n",
       "         1.38501285e+00],\n",
       "       [-1.28136534e+00,  9.04719988e-02, -1.23468093e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-6.98926548e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 1.16487758e-01, -1.89780797e+00,  9.72522648e-02,\n",
       "        -3.01465776e-01],\n",
       "       [-3.49463274e-01, -7.93207989e-01,  2.08246698e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.63082861e+00, -1.67688798e+00, -1.40117258e+00,\n",
       "        -1.20956965e+00],\n",
       "       [ 1.51434085e+00,  1.19507198e+00,  1.26269381e+00,\n",
       "         1.64447110e+00],\n",
       "       [ 5.82438790e-01, -3.51367995e-01,  2.63743914e-01,\n",
       "         8.77215987e-02],\n",
       "       [-2.32975516e-01, -1.01412799e+00, -1.80233817e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 2.09677965e+00,  1.63691198e+00,  1.59567711e+00,\n",
       "         1.25528372e+00],\n",
       "       [-1.16487758e+00, -1.23504798e+00,  3.74738347e-01,\n",
       "         6.06638098e-01],\n",
       "       [-1.04838982e+00,  9.74151987e-01, -1.23468093e+00,\n",
       "        -8.20382275e-01],\n",
       "       [ 4.13847651e-15, -1.30447998e-01,  2.08246698e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.04838982e+00,  9.74151987e-01, -1.40117258e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.51434085e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  9.85207726e-01,\n",
       "         1.51474197e+00],\n",
       "       [-9.31902064e-01,  1.63691198e+00, -1.06818928e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-2.32975516e-01,  2.96243196e+00, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-5.82438790e-01,  1.85783197e+00, -1.40117258e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-1.04838982e+00,  7.53231990e-01, -1.23468093e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  3.19241130e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 1.16487758e-01, -3.51367995e-01,  3.74738347e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.39785310e+00,  3.11391996e-01, -1.23468093e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 9.31902064e-01, -1.23504798e+00,  1.09620216e+00,\n",
       "         7.36367223e-01],\n",
       "       [-2.32975516e-01, -5.72287992e-01,  1.52749481e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.16487758e-01, -7.93207989e-01,  7.07721645e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 4.65951032e-01,  5.32311993e-01,  1.20719659e+00,\n",
       "         1.64447110e+00],\n",
       "       [-1.16487758e-01, -1.01412799e+00,  9.72522648e-02,\n",
       "        -4.20075261e-02],\n",
       "       [ 1.51434085e+00, -1.30447998e-01,  1.09620216e+00,\n",
       "         4.76908973e-01],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  4.85732779e-01,\n",
       "         3.47179848e-01],\n",
       "       [-9.31902064e-01, -1.23504798e+00, -4.57719899e-01,\n",
       "        -1.71736651e-01],\n",
       "       [-1.04838982e+00, -2.33964797e+00, -1.80233817e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 1.16487758e+00,  3.11391996e-01,  1.04070494e+00,\n",
       "         1.38501285e+00],\n",
       "       [-1.16487758e-01,  2.07875197e+00, -1.45666979e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01, -1.89780797e+00,  3.74738347e-01,\n",
       "         3.47179848e-01],\n",
       "       [-2.32975516e-01, -3.51367995e-01,  2.08246698e-01,\n",
       "         8.77215987e-02],\n",
       "       [ 2.09677965e+00, -1.01412799e+00,  1.70667154e+00,\n",
       "         1.38501285e+00],\n",
       "       [ 4.65951032e-01, -3.51367995e-01,  9.85207726e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 4.65951032e-01, -5.72287992e-01,  7.07721645e-01,\n",
       "         3.47179848e-01],\n",
       "       [ 2.32975516e-01, -5.72287992e-01,  4.85732779e-01,\n",
       "        -4.20075261e-02],\n",
       "       [-3.49463274e-01, -1.30447998e-01,  1.52749481e-01,\n",
       "         8.77215987e-02],\n",
       "       [ 5.82438790e-01, -5.72287992e-01,  9.85207726e-01,\n",
       "         1.12555460e+00],\n",
       "       [ 8.15414306e-01, -1.30447998e-01,  3.19241130e-01,\n",
       "         2.17450723e-01],\n",
       "       [-1.16487758e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.46902790e+00],\n",
       "       [-1.74731637e+00, -3.51367995e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 5.82438790e-01,  9.04719988e-02,  9.29710510e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 5.82438790e-01, -7.93207989e-01,  8.18716077e-01,\n",
       "         8.66096347e-01],\n",
       "       [-9.31902064e-01,  5.32311993e-01, -1.17918371e+00,\n",
       "        -9.50111400e-01],\n",
       "       [-8.15414306e-01,  7.53231990e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.16487758e+00, -1.45596798e+00, -2.91228250e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 9.31902064e-01, -1.30447998e-01,  6.52224428e-01,\n",
       "         6.06638098e-01],\n",
       "       [ 6.98926548e-01, -1.30447998e-01,  7.63218861e-01,\n",
       "         9.95825472e-01],\n",
       "       [-1.16487758e-01, -7.93207989e-01,  7.07721645e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 1.16487758e+00,  9.04719988e-02,  5.96727212e-01,\n",
       "         3.47179848e-01],\n",
       "       [-5.82438790e-01,  1.85783197e+00, -1.17918371e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-8.15414306e-01, -7.93207989e-01,  4.17550485e-02,\n",
       "         2.17450723e-01],\n",
       "       [-3.49463274e-01, -5.72287992e-01,  5.96727212e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 1.74731637e+00, -5.72287992e-01,  1.26269381e+00,\n",
       "         8.66096347e-01],\n",
       "       [-2.32975516e-01, -1.30447998e-01,  2.08246698e-01,\n",
       "        -4.20075261e-02],\n",
       "       [ 1.63082861e+00, -3.51367995e-01,  1.37368824e+00,\n",
       "         7.36367223e-01],\n",
       "       [ 4.65951032e-01, -1.23504798e+00,  6.52224428e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 1.04838982e+00, -5.72287992e-01,  5.41229996e-01,\n",
       "         2.17450723e-01],\n",
       "       [-1.51434085e+00,  3.11391996e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.16487758e+00,  1.19507198e+00, -1.34567536e+00,\n",
       "        -1.46902790e+00],\n",
       "       [ 1.16487758e-01,  7.53231990e-01,  3.74738347e-01,\n",
       "         4.76908973e-01],\n",
       "       [ 5.82438790e-01, -5.72287992e-01,  9.85207726e-01,\n",
       "         1.25528372e+00],\n",
       "       [-1.04838982e+00,  3.11391996e-01, -1.45666979e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01, -5.72287992e-01,  5.41229996e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 4.13847651e-15, -1.30447998e-01,  7.07721645e-01,\n",
       "         7.36367223e-01],\n",
       "       [-5.82438790e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 5.82438790e-01,  3.11391996e-01,  8.18716077e-01,\n",
       "         1.38501285e+00],\n",
       "       [-5.82438790e-01,  7.53231990e-01, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 2.32975516e-01, -3.51367995e-01,  4.85732779e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 2.32975516e-01, -1.01412799e+00,  9.85207726e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 4.65951032e-01, -1.23504798e+00,  5.96727212e-01,\n",
       "         3.47179848e-01]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#训练集标准化\n",
    "scaler = StandardScaler()\n",
    "X_train_std = scaler.fit_transform(Xtrain)\n",
    "X_train_std\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "21ef8e30",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr = LogisticRegression(penalty='l2',solver='liblinear', \n",
    "                       C= 0.5,\n",
    "                       max_iter=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "61518a7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.5, max_iter=1000, solver='liblinear')"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.fit(X_train_std,Ytrain)#训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "53ee7495",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8857142857142857"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.score(X_train_std,Ytrain)#得分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "c3b94f1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.53220047e-01, -1.37279645e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 1.08786233e+00, -1.37279645e-01,  1.33560386e+00,\n",
       "         1.48206510e+00],\n",
       "       [ 2.60474080e+00, -1.37279645e-01,  1.82176906e+00,\n",
       "         1.34455390e+00],\n",
       "       [-1.39430243e+00,  3.77519023e-01, -1.39907542e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-9.80608300e-01,  8.92317690e-01, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.22576037e-01, -9.09477646e-01,  3.02502794e-01,\n",
       "        -1.68069238e-01],\n",
       "       [-7.04812216e-01,  1.14971702e+00, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01, -1.16687698e+00,  4.84814746e-01,\n",
       "         1.06953151e-01],\n",
       "       [ 3.98372122e-01, -1.37279645e-01,  7.27897349e-01,\n",
       "         9.32020318e-01],\n",
       "       [ 5.36270164e-01, -1.37279645e-01,  6.06356048e-01,\n",
       "         3.81975540e-01],\n",
       "       [ 1.63945450e+00,  1.20119689e-01,  1.09252125e+00,\n",
       "         1.34455390e+00],\n",
       "       [-4.29016131e-01, -1.37279645e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [-1.53220047e+00,  1.40711636e+00, -1.58138738e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-9.80608300e-01, -1.37279645e-01, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.50155646e+00, -1.37279645e-01,  1.15329190e+00,\n",
       "         1.34455390e+00],\n",
       "       [ 1.22576037e-01, -9.09477646e-01,  1.80961493e-01,\n",
       "         1.06953151e-01],\n",
       "       [-1.80799655e+00,  3.77519023e-01, -1.39907542e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-1.53220047e+00,  8.92317690e-01, -1.33830477e+00,\n",
       "        -1.13064760e+00],\n",
       "       [-1.25640438e+00,  8.92317690e-01, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-8.42710258e-01,  1.92191502e+00, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-4.29016131e-01,  8.92317690e-01, -1.15599282e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.36365842e+00, -1.37279645e-01,  9.70979951e-01,\n",
       "         1.61957629e+00],\n",
       "       [-2.91118089e-01, -1.42427631e+00,  2.41732144e-01,\n",
       "         2.44464346e-01],\n",
       "       [-1.25640438e+00, -1.37279645e-01, -1.33830477e+00,\n",
       "        -1.40566999e+00],\n",
       "       [ 1.08786233e+00, -6.52078312e-01,  6.06356048e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 8.12066248e-01,  6.34918356e-01,  6.67126698e-01,\n",
       "         6.56997929e-01],\n",
       "       [-8.42710258e-01,  8.92317690e-01, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01,  2.95151236e+00, -1.33830477e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01, -1.68167565e+00,  5.94201917e-02,\n",
       "        -1.68069238e-01],\n",
       "       [ 1.22576037e+00, -3.94678978e-01,  6.06356048e-01,\n",
       "         2.44464346e-01],\n",
       "       [-9.80608300e-01, -1.93907498e+00, -1.83662411e-01,\n",
       "        -1.68069238e-01],\n",
       "       [ 8.12066248e-01, -9.09477646e-01,  7.88667999e-01,\n",
       "         9.32020318e-01],\n",
       "       [ 1.36365842e+00,  6.34918356e-01,  1.27483320e+00,\n",
       "         1.34455390e+00],\n",
       "       [-9.80608300e-01,  6.34918356e-01, -1.33830477e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-8.42710258e-01,  1.14971702e+00, -1.33830477e+00,\n",
       "        -1.13064760e+00],\n",
       "       [ 9.49964291e-01,  3.77519023e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 8.12066248e-01,  8.92317690e-01,  1.21406255e+00,\n",
       "         1.75708748e+00],\n",
       "       [-1.53220047e-01, -3.94678978e-01, -1.35045890e-03,\n",
       "         2.44464346e-01],\n",
       "       [ 2.60474080e-01,  3.77519023e-01,  7.27897349e-01,\n",
       "         9.32020318e-01],\n",
       "       [-1.53220047e-02, -6.52078312e-01,  5.45585397e-01,\n",
       "         2.44464346e-01],\n",
       "       [-2.91118089e-01, -1.68167565e+00,  1.20190842e-01,\n",
       "        -3.05580432e-02],\n",
       "       [ 6.74168206e-01, -3.94678978e-01,  4.24044096e-01,\n",
       "         2.44464346e-01],\n",
       "       [-2.91118089e-01, -1.93907498e+00,  2.41732144e-01,\n",
       "         2.44464346e-01],\n",
       "       [ 3.98372122e-01, -9.09477646e-01,  9.10209301e-01,\n",
       "         6.56997929e-01],\n",
       "       [ 1.08786233e+00, -1.37279645e-01,  1.15329190e+00,\n",
       "         9.32020318e-01]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#测试集标准化\n",
    "scaler = StandardScaler()\n",
    "X_test_std = scaler.fit_transform(Xtest)\n",
    "X_test_std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "b6611abe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7555555555555555"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.score(X_test_std,Ytest)#测试集得分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd83b28d",
   "metadata": {},
   "source": [
    "# 确定l2正则化情况下，网格搜索确定solver,C的最佳组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "d6c0b594",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=LogisticRegression(max_iter=10000),\n",
       "             param_grid={'C': [0.05, 0.10277777777777777, 0.15555555555555556,\n",
       "                               0.20833333333333331, 0.2611111111111111,\n",
       "                               0.3138888888888889, 0.36666666666666664,\n",
       "                               0.41944444444444445, 0.4722222222222222, 0.525,\n",
       "                               0.5777777777777778, 0.6305555555555556,\n",
       "                               0.6833333333333333, 0.7361111111111112,\n",
       "                               0.788888888888889, 0.8416666666666667,\n",
       "                               0.8944444444444445, 0.9472222222222223, 1.0],\n",
       "                         'solver': ['liblinear', 'sag', 'newton-cg', 'lbfgs']})"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#在l2范式下，判断C和solver的最优值\n",
    "p = {\n",
    "    'C':list(np.linspace(0.05,1,19)),\n",
    "    'solver':['liblinear','sag','newton-cg','lbfgs']\n",
    "}\n",
    "\n",
    "model = LogisticRegression(penalty='l2',max_iter=10000)\n",
    "\n",
    "GS = GridSearchCV(model,p,cv=5)\n",
    "GS.fit(X_train_std,Ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "2d4804cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9714285714285715, {'C': 0.41944444444444445, 'solver': 'sag'})"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_score_,GS.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c94dedd9",
   "metadata": {},
   "source": [
    "# 将最优的结果重新用来实例化模型，查看训练集和测试集下的分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "93c3de15",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr_best = LogisticRegression(penalty='l2',solver=GS.best_params_['solver'],C=GS.best_params_['C'],max_iter=10000)#实例化对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "f3d99acf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.41944444444444445, max_iter=10000, solver='sag')"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_best.fit(X_train_std,Ytrain)#训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "cfac7e11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9714285714285714"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_best.score(X_train_std,Ytrain)#训练得分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "bc8fdf21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8444444444444444"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_best.score(X_test_std,Ytest)#测试集得分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6aae5fef",
   "metadata": {},
   "source": [
    "# 计算精准率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "5631b7dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, 0, 0, 1, 0, 1, 2, 1, 2, 2, 0, 0, 2, 1, 0, 0, 0, 0, 0, 2,\n",
       "       1, 0, 2, 2, 0, 0, 1, 1, 1, 2, 2, 0, 0, 2, 2, 1, 2, 1, 1, 1, 1, 2,\n",
       "       2])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_pd = lr_best.predict(X_test_std)#预测值\n",
    "lr_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "5f16d466",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(lr_pd[lr_pd==Ytest]==0).sum()/(lr_pd[lr_pd==lr_pd]==0).sum()#计算分类为0的精准率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "1e0d2400",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(lr_pd[lr_pd==Ytest]==1).sum()/(lr_pd[lr_pd==lr_pd]==1).sum()#计算分类为0的精准率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "7e64084e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5882352941176471"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(lr_pd[lr_pd==Ytest]==2).sum()/(lr_pd[lr_pd==lr_pd]==2).sum()#计算分类为2的精准率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0ae7382",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d143a798",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec6e70ec",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
